package com.vitalfit.controller;

import com.vitalfit.enums.ResultEnum;
import com.vitalfit.model.dto.FoodCustomizationDTO;
import com.vitalfit.model.entity.food.FoodCustomization;
import com.vitalfit.model.vo.ResultVO;
import com.vitalfit.service.FoodCustomizationService;
import com.vitalfit.utils.ResultVOUtil;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/food-customizations")
@RequiredArgsConstructor
@Validated
public class FoodCustomizationController {

    private final FoodCustomizationService foodCustomizationService;

    /**
     * 获取指定食品ID的所有定制选项
     * @param foodItemId 食品ID
     * @return 定制选项列表
     */
    @PreAuthorize("hasRole('ROLE_SHOPMANAGER') or hasRole('ROLE_CUSTOMER')")
    @GetMapping("/{foodItemId}")
    public ResultVO<List<FoodCustomization>> getCustomizationsByFoodItemId(@PathVariable Integer foodItemId) {
        List<FoodCustomization> customizations = foodCustomizationService.findCustomizationsByFoodItemId(foodItemId);
        return ResultVOUtil.buildResult(ResultEnum.GET_SUCCESS,customizations);
    }

    /**
     * 创建新的食品定制项
     * @param foodCustomizationDTO 食品定制数据传输对象
     * @return 新创建的食品定制项
     */
    @PreAuthorize("hasRole('ROLE_SHOPMANAGER')")
    @PostMapping
    public ResultVO<FoodCustomization> createCustomization(@Valid @RequestBody FoodCustomizationDTO foodCustomizationDTO) {
        FoodCustomization created = foodCustomizationService.createCustomization(foodCustomizationDTO);
        return ResultVOUtil.buildResult(ResultEnum.POST_SUCCESS,created);
    }

    /**
     * 更新指定ID的食品定制项
     * @param foodCustomizationDTO 食品定制数据传输对象
     * @return 更新后的食品定制项
     */
    @PreAuthorize("hasRole('ROLE_SHOPMANAGER')")
    @PutMapping("/{id}")
    public ResultVO<FoodCustomization> updateCustomization(@Valid @RequestBody FoodCustomizationDTO foodCustomizationDTO) {
        FoodCustomization updated = foodCustomizationService.updateCustomization(foodCustomizationDTO);
        return ResultVOUtil.buildResult(ResultEnum.PUT_SUCCESS,updated);
    }

    /**
     * 删除指定ID的食品定制项
     * @param id 食品定制项ID
     * @return 响应状态
     */
    @PreAuthorize("hasRole('ROLE_SHOPMANAGER')")
    @DeleteMapping("/{id}")
    public ResultVO<Void> deleteCustomization(@PathVariable Integer id) {
        foodCustomizationService.deleteCustomization(id);
        return ResultVOUtil.buildResult(ResultEnum.DELETE_SUCCESS,null);

    }

    @PreAuthorize("hasRole('ROLE_SHOPMANAGER') or hasRole('ROLE_CUSTOMER')")
    @GetMapping("/{id}")
    public ResultVO<FoodCustomization> getCustomization(@PathVariable Integer id) {
        return ResultVOUtil.buildResult(ResultEnum.GET_SUCCESS,foodCustomizationService.getCustomizationById(id));

    }
}
